<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

require $_SERVER['DOCUMENT_ROOT'].'/../_interface/upload.php';

$db = new DB();

$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$title = isset($_POST['title']) ? h($_POST['title']) : '';
$code = isset($_POST['code']) ? h($_POST['code']) : '';
$orders = isset($_POST['orders']) ? intval($_POST['orders']) : 0;
$edit_uid = Sessions::get('uid');
$updated = time();

//检验必要参数
if( $id<1 || $title=='' || $code=='' ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

//检验标题是否重复
$sql = 'select id from analytic_codes where title="'.$title.'" and is_del=1 limit 1';
$rs = $db->query($sql)->getOne();
if( $rs['id'] > 0 && $rs['id'] != $id ){
	echo '<script>alert("添加失败，《'.$title.'》已存在！");window.history.go(-1);</script>';
	exit;
}

//组装insert数组
$data = array('title'=>$title,
				'code'=>$code,
				'orders'=>$orders,
				'edit_uid'=>$edit_uid,
				'updated'=>$updated,
				);
$db->table('analytic_codes')->where('id='.$id)->update($data)->run();

echo '<script>alert("修改成功！");window.location.href="/www/admin/analytic_code/code_lists.php";</script>';
exit;